package com.sunfeng.elasticsearch.document.search;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;


/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2022/4/22  17:31
 */
public class ESTest_Doc_Query_DSL {
    private static RestHighLevelClient client;

    static {
        client = new RestHighLevelClient(
                RestClient.builder(new HttpHost("192.168.139.187", 9200, "http"))
        );
    }


    public static void main(String[] args) throws IOException {
        // 创建客户端对象

        simpleQueryStringQuery(client);
        // aggregationsTest(client);
        client.close();
    }

    /**
     * matchQuery  通过分词查询 是or的关系
     *
     * @param client
     * @throws IOException
     */
    private static void matchQuery(RestHighLevelClient client) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchQuery("name", "wangwu lisi"));

        searchRequest.source(query);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        print(searchResponse);
    }

    /**
     * matchQuery  通过分词查询 是and的关系
     * @param client
     * @throws IOException
     */
    private static void matchQueryAnd(RestHighLevelClient client) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");
        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchQuery("name", "wangwu  lisi").operator(Operator.AND));
        searchRequest.source(query);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        print(searchResponse);
    }

    private static void matchPhraseQuery(RestHighLevelClient client) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");

        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchPhraseQuery("name", "wangwu  lisi").slop(1));
        searchRequest.source(query);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        print(searchResponse);
    }
    private static void simpleQueryStringQuery(RestHighLevelClient client) throws IOException {
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.indices("user");

        SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.simpleQueryStringQuery("wangwu lisi").field("name").defaultOperator(Operator.AND));
        searchRequest.source(query);
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        print(searchResponse);
    }
    private static void print(SearchResponse searchResponse) {
        SearchHits hits = searchResponse.getHits();
        System.out.println(hits.getTotalHits());
        System.out.println(searchResponse.getTook());

        for (SearchHit searchHit : hits) {
            System.out.println(searchHit.getSourceAsString());
        }
    }

}
